package com.example.yzx.allriddle;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import com.example.yzx.allriddle.daomain.QApp;
import com.example.yzx.allriddle.utils.Db;

public class LoginActivity extends AppCompatActivity {
    private TextView sc;
    private TextView user;
    private TextView pass;
    private Button login;
    private Db db = new Db(this, 1);
    private QApp app;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }
        SharedPreferences userSettings1= getSharedPreferences("setting", 0);
        String names = userSettings1.getString("name","默认值");
        if(names.equals("yes")){
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            intent.putExtra("user", "yes");
            startActivity(intent);
        }
        app= (QApp) getApplication();
        System.out.println(app.getserid());
        sc=(TextView) findViewById(R.id.zhuche);
        user=findViewById(R.id.account);
        pass=findViewById(R.id.pass);
        login=findViewById(R.id.login);
        sc.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                if(login.getText().toString().equals("登录")) {
                    login.setText("注册");
                    sc.setText("登录");
                }else{
                    login.setText("登录");
                    sc.setText("注册");
                }
            }
        });
    }
    //登录注册模块
     public  void logins(View v){
        AlertDialog alertDialog1 = new AlertDialog.Builder(this)
                .setTitle("用户名或密码不能为空")//标题
                .setMessage(" ")//内容
                .create();

         if (user.getText().toString().equals("") && pass.getText().toString().equals("")) {
             alertDialog1.show();
         } else {
             if (login.getText().toString().equals("登录")) {
                 SQLiteDatabase sqLiteDbup = db.getReadableDatabase();
                 Cursor c = sqLiteDbup.query("user", new String[]{"uid", "name", "word"}, null, null, null, null, null);
                 boolean sign = true;
                 while (c.moveToNext()) {
                     Integer id = c.getInt(c.getColumnIndex("uid"));
                     String name = c.getString(c.getColumnIndex("name"));
                     String word = c.getString(c.getColumnIndex("word"));
                     if (name.equals(user.getText().toString()) && word.equals(pass.getText().toString())) {
                         System.out.println("success");
                         sqLiteDbup.close();

                         app.setUserid(id);
                         sign = false;
                         //记住密码存放数据
                         SharedPreferences userSettings = getSharedPreferences("setting", 0);
                         SharedPreferences.Editor editor = userSettings.edit();
                         editor.putString("name", "yes");
                         editor.commit();
                     }
                 }

                 if (sign) {
                     AlertDialog alertDialog2 = new AlertDialog.Builder(this)
                             .setTitle("用户名或密码错误")//标题
                             .setMessage(" ")//内容
                             .create();
                     alertDialog2.show();
                 } else {
                     Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                     intent.putExtra("user", "yes");
                     startActivity(intent);
                 }
             } else if (login.getText().toString().equals("注册")) {
                 System.out.println("插入数据");
                 SQLiteDatabase sqLiteDb = db.getWritableDatabase();
                 sqLiteDb = db.getWritableDatabase();
                 ContentValues cv = new ContentValues();
                 cv.put("name", user.getText().toString() + "");
                 cv.put("word", pass.getText().toString() + "");
                 cv.put("grade", -1);
                 sqLiteDb.insert("user", null, cv);
                 sqLiteDb.close();
                 AlertDialog alertDialog2 = new AlertDialog.Builder(this)
                         .setTitle("注册成功")//标题
                         .setMessage(" ")//内容
                         .create();
                 alertDialog2.show();

           /*删除数据
            sqLiteDb = db.getWritableDatabase();
            sqLiteDb.delete("user","id!=1",null);*/
             }

         }
     }

}
